<!-- <template>
  <div class="clearfix" id="login_wrap">
    <h2 class="title">登录页面</h2>
    <div class="login">
      <div class="login-account">
        <span>帐号</span>
        <input type="text" placeholder="输入用户名" name="account" v-model.trim="account">
      </div>
      <div class="login-password">
        <span>密码</span>
        <input type="password" placeholder="输入密码" name="password" v-model.trim="password" @keyup.enter="login">
      </div>
      <p class="login-btn">
        <button id="loginbtn" @click="login">登录</button>
      </p>
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  onMounted
} from 'vue'
import store from '@/store'
import { useRouter } from 'vue-router'
import { Login } from "@/api/login"
// import DynamicRoutes, { userObj } from '@/router/dynamic-router'
// import { recursionRouter, selectArrayByNames } from '@/utlis/recursion-route'
// import _ from 'lodash'


// const account = ref('sczgss')
// const password = ref('sczgss123.')
const account = ref('13880442044')
const password = ref('sczgss123.')
// const account = ref('hzbyd')
// const password = ref('110235')
const router = useRouter()
//登录请求
const login = async () => {
  //再登录请求中拿到token
  const res = await Login({
    account: account.value,
    password: password.value
  }).then(r => {
    console.log(r, '登录的数据')
    if (r) {
      //存储到vuex中 
      store.commit('LOGIN_IN', r.token)
      store.commit('USER_INFO', JSON.stringify(r.user))
      sessionStorage.setItem('token', r.token)
      sessionStorage.setItem('account', r.user)
      console.log(router, '所有路由');
      router.push('/main')
    }
  })
}
// onMounted(() => {
//   let allRoues = _.cloneDeep(DynamicRoutes)
//   let userRouter = _.cloneDeep(userObj)

//   let route = recursionRouter(userRouter, allRoues)
//   console.log(route, '---------------');
// })
</script>

<style lang="less" scoped></style> -->
<template>
  <div class="login-img">
    <div class="manage-admin">
      <div class="mask">
        <h1>聚新鲜</h1>
        <p>无人售货</p>
        <img src="https://www.sczgss.com/addons/vending_machine/icon.jpg" alt="">
      </div>
      <div class="login">
        <div class="form-content">
          <div class="form-item">
            <div class="label-item">用户名：</div>
            <div>
              <el-input v-model.trim="ruleForm.account" placeholder="请输入用户名" type="text" style="width: 240px;"
                autocomplete="off" />
            </div>
          </div>
          <div class="form-item">
            <div class="label-item">密码：</div>
            <div>
              <el-input v-model.trim="ruleForm.password" placeholder="请输入密码" type="password" style="width: 240px;"
                autocomplete="off" @keyup.enter="submitForm" />
            </div>
          </div>
          <div class="form-item">
            <div class="label-item"></div>
            <div>
              <el-button type="primary" @click="submitForm" class="login-btn">登录</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ElMessage } from 'element-plus';
import { Login } from '../api/login'
import { getUserMenu } from '@/api/public'
import { reactive, onMounted, } from 'vue'
import store from '@/store';
import router from '@/router';
import _ from 'lodash'


const ruleForm = reactive({
  account: '',
  password: '',
})

const rules = reactive({
  account: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
})

onMounted(() => {
  console.log(router.options.routes);
})
const loginFun = async () => {
  if (ruleForm.account == '') {
    ElMessage.error('用户名必须填写')
    return
  } else if (ruleForm.password == '') {
    ElMessage.error('密码必须填写')
    return
  }
  const success = await Login(ruleForm).then(res => {
    let { code, data } = res
    if (code == 200) {
      //存储数据
      sessionStorage.setItem('token', data.token)
      sessionStorage.setItem('account', data.user)
      console.log(data.user, '用户信息');
      store.commit('LOGIN_IN', data.token)
      store.commit('USER_INFO', JSON.stringify(data.user))
      getUserMenu().then(r => {
        const { code, data } = r
        sessionStorage.setItem('userMenu', JSON.stringify(data))
        store.commit('USER_MENU', JSON.stringify(data))
        // router.push('/controlBooth')
        console.log(store.state.userMenu, '侧边栏的数据');
        // return
        router.push('home/controlBooth')
      }).catch((error) => {

      })

    }
  }).catch((err) => {

  })
}
const submitForm = _.throttle(loginFun, 3000)
</script>
<style lang="less" scoped>
.login-img {
  width: 100vw;
  height: 100vh;
  position: relative;
  background: linear-gradient(to top right, rgb(97, 97, 225), rgb(211, 150, 160));

  .el-form {
    margin-right: 40px;
  }
}

.manage-admin {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 1000px;
  height: 500px;
  border-radius: 10px;
  box-shadow: 5px 4px 5px rgba(0, 0, 0, .6);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

@media screen and (min-width: 768px) {
  .manage-admin {
    width: 800px;
  }
}

@media screen and (min-width: 992px) {
  .manage-admin {
    width: 1000px;
  }
}

.login {
  position: relative;
  width: 50%;
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.createAdmin {
  position: relative;
}

span {
  position: absolute;
  bottom: 20px;
  right: 100px;
  cursor: pointer;
}

.mask {
  width: 50%;
  height: 100%;
  background-color: rgb(143, 143, 224);
  border-radius: 10px;
  box-shadow: 5px 4px 5px rgba(0, 0, 0, .4);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.mask h1 {
  height: 100px;
  line-height: 150px;
  font-size: 28px;
  letter-spacing: 4px;
  text-shadow: 5px 4px 5px rgba(0, 0, 0, .4);
}

.mask p {
  margin: 10px 0 10px 0;
  font-size: 18px;
  letter-spacing: 4px;
  font-weight: bold;
  text-shadow: 5px 4px 5px rgba(0, 0, 0, .4);
}

.mask img {
  text-align: center;
  width: 200px;
  height: 200px;
  border-radius: 50%;

}

.form-content {
  width: 400px;

  .form-item {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    margin-bottom: 20px;

    .label-item {
      width: 100px;
      font-size: 16px;
      color: #fff;
      text-align: center;
      letter-spacing: 2px;
    }
  }
}

.login-btn {
  padding: 10px 100px;
  font-size: 14px;
}

:deep(.el-form-item .el-form-item__label) {
  font-size: 16px;
  color: #fff;
  font-size: 16px;
  letter-spacing: 2px;
}

:deep(.el-button) {
  letter-spacing: 4px;
}
</style>